package com.zmn.oms.dubbo.impl.zmn.bcustomer.order;

import org.apache.dubbo.config.annotation.Service;
import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.dto.ResponseDTO;
import com.zmn.common.utils.reflect.BeanMapper;
import com.zmn.common.utils.response.ResponseUtil;
import com.zmn.common.utils.validator.ValidateResult;
import com.zmn.common.utils.validator.ValidatorUtil;
import com.zmn.oms.business.interfaces.base.BaseCodeService;
import com.zmn.oms.common.constant.OrderConsts;
import com.zmn.oms.common.exception.OmsBaseException;
import com.zmn.oms.dubbo.dto.zmn.order.*;
import com.zmn.oms.dubbo.impl.zmn.order.OrderBaseRemoteService;
import com.zmn.oms.dubbo.interfaces.zmn.bcustomer.order.ZsBCustomerOrderRemoteService;
import com.zmn.oms.dubbo.utils.DubboConsts;
import com.zmn.oms.model.dto.order.zmn.ZsNormalNewOrderDTO;
import com.zmn.oms.model.dto.order.zmn.ZsNormalUpdateOrderDTO;
import com.zmn.oms.model.vo.work.ZsNormalUpdateOrderVO;
import com.zmn.oms.zmn.business.interfaces.work.ZsOrderWorkBService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * 类描述: 大客户单
 *
 * @author liuying
 * @date 2019/01/03 3:21 PM
 */
@Service(loadbalance = "roundrobin", timeout = 20000, retries = 2, version = DubboConsts.INTERFACE_VERSION, validation = "false")
public class ZsBCustomerOrderRemoteServiceImpl extends OrderBaseRemoteService implements ZsBCustomerOrderRemoteService {

    private static final String TAG = "啄木鸟-大客户-新增订单";
    private Logger logger = LoggerFactory.getLogger(ZsBCustomerOrderRemoteServiceImpl.class);

    @Resource
    private BaseCodeService baseCodeService;


    @Override
    public ResponseDTO cancelOrder(CancelOrderDIO cancelOrderDIO) {
        logger.info("[{}] 取消订单：{}", TAG, cancelOrderDIO);

        ValidateResult validateResult = ValidatorUtil.validator(cancelOrderDIO);
        if (!validateResult.isSuccess()) {
            logger.error("[{}] 参数错误：{}", TAG, validateResult.toString());
            return ResponseUtil.responseFail("提交参数错误");
        }

        // 操作人类型 大客户
        cancelOrderDIO.setOperatorType(GlobalConsts.OPERATE_USER_TYPE_BCUSTOMER);

        return super.cancelOrder(cancelOrderDIO);
    }

    @Override
    public ResponseDTO updateOrder(BCustomerUpdateOrderDIO bCustomerNewOrderDIO) {
        logger.info("[{}] 修改订单：{}", TAG, bCustomerNewOrderDIO);

        ValidateResult validateResult = ValidatorUtil.validator(bCustomerNewOrderDIO);
        if (!validateResult.isSuccess()) {
            logger.error("[{}] 参数错误：{}", TAG, validateResult.toString());
            return ResponseUtil.responseFail("提交参数错误：" + validateResult.toString());
        }

        // DIO转换
        ZsNormalUpdateOrderDTO normalUpdateOrderDTO = BeanMapper.map(bCustomerNewOrderDIO, ZsNormalUpdateOrderDTO.class);
        normalUpdateOrderDTO.setOperator(bCustomerNewOrderDIO.getUserName());
        normalUpdateOrderDTO.setOperatorId(bCustomerNewOrderDIO.getUserId());
        normalUpdateOrderDTO.setOperatorType(GlobalConsts.OPERATE_USER_TYPE_BCUSTOMER);

        // 大客户单渠道ID
        try {
            normalUpdateOrderDTO.setChannelId(baseCodeService.getChannelId(GlobalConsts.PLAT_MARK_ZMN, GlobalConsts.BASE_CODE_MAP_TWO_TYPE_OMS_CHANNEL_MAJOR));
        } catch (OmsBaseException e) {
            return ResponseUtil.responseFail("大客户渠道配置错误");
        }

        return super.updateOrder(normalUpdateOrderDTO);
    }

    @Override
    public ResponseDTO<NewOrderDRO> addOrder(BCustomerNewOrderDIO newOrderDIO) {
        logger.info("[{}] 添加订单：{}", TAG, newOrderDIO);

        ValidateResult validateResult = ValidatorUtil.validator(newOrderDIO);
        if (!validateResult.isSuccess()) {
            logger.error("[{}] 参数错误：{}", TAG, validateResult.toString());
            return ResponseUtil.responseFail("提交参数错误：" + validateResult.toString());
        }

        // DIO转换
        ZsNormalNewOrderDTO normalNewOrderDTO = BeanMapper.map(newOrderDIO, ZsNormalNewOrderDTO.class);
        normalNewOrderDTO.setOperator(newOrderDIO.getUserName());
        normalNewOrderDTO.setOperatorId(newOrderDIO.getUserId());
        normalNewOrderDTO.setOperatorType(GlobalConsts.OPERATE_USER_TYPE_BCUSTOMER);
        normalNewOrderDTO.setReceiverManner(OrderConsts.RECEIVE_MANNER_MANUAL);
        // 大客户单
        normalNewOrderDTO.setSourceType(OrderConsts.ORDER_SOURCE_TYPE_BCUSTOMER);

        // 大客户单渠道ID
        try {
            normalNewOrderDTO.setChannelId(baseCodeService.getChannelId(GlobalConsts.PLAT_MARK_ZMN, GlobalConsts.BASE_CODE_MAP_TWO_TYPE_OMS_CHANNEL_MAJOR));
        } catch (OmsBaseException e) {
            return ResponseUtil.responseFail("大客户渠道配置错误");
        }
        logger.info("未替换下单接口：[com.zmn.oms.dubbo.impl.zmn.bcustomer.order.ZsBCustomerOrderRemoteServiceImpl.addOrder]，下单渠道：[{}]，下单方式：[{}]", normalNewOrderDTO.getChannelId(), normalNewOrderDTO.getReceiveEntranceId());
        return super.addOrder(normalNewOrderDTO);
    }

}
